import os
import json
import wx
from PIL import Image
from pathlib import Path

from tools.tools import init_db, read_db, save_db, resize, pilimg_to_wxbitimage


class Myframe(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u'图片模糊分类', size=(640, 640))
        self.p = wx.Panel(self)
        self.font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        self.SetFont(self.font)
        self.font.SetPointSize(15)

        self.save_folder = Path('../data/output')
        self.img_folder = Path('../data/input')

        self.init_frame()

    def init_db(self, img_folder, save_folder):



    def init_frame(self):
        self.p.DestroyChildren()
        self.width, self.height = self.GetSize()
        up_button = wx.Button(self.p, label=u'上一张', pos=(0, 0), size=(60, -1))
        down_button = wx.Button(self.p, label=u'下一张', pos=(65, 0), size=(60, -1))
        save_folder_button = wx.Button(self.p, label=u'Save Folder', pos=(self.width-60, 0), size=(60, -1))
        img_folder_button = wx.Button(self.p, label=u'Image Folder', pos=(self.width-125, 0), size=(60, -1))

        self.Bind(wx.EVT_BUTTON, self.on_up, up_button)
        self.Bind(wx.EVT_BUTTON, self.on_down, down_button)
        self.Bind(wx.EVT_BUTTON, self.on_save_folder, save_folder_button)
        self.Bind(wx.EVT_BUTTON, self.on_img_folder, img_folder_button)


    def start(self):
        self.p.DestroyChildren()

        self.dic, self.dicts = get_new_data(db_file=self.db_file)
        self.path = str(self.data_base / self.dic['root'] / self.dic['file_name'])

        self.width, self.height = self.GetSize()
        image = Image.open(self.path)
        x, y = image.size
        x = self.width / 2 - x / 2
        y = self.height / 3 - y / 2
        pic = wx.Image(self.path, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        wx.StaticBitmap(self.p, pos=(x, y), bitmap=pic)

        self.input_text = wx.TextCtrl(self.p, -1, self.dic['file_txt'], size=(350, 50), pos=(x - 50, y + 100),
                                      style=wx.TE_MULTILINE)
        # self.input_text.SetInsertionPoint(-1)
        self.input_text.SetSelection(0, -1)
        self.input_text.SetFont(self.font)
        self.Bind(wx.EVT_TEXT, self.on_txt, self.input_text)

        self.lbl = wx.StaticText(self.p, -1, label=self.dic['file_name'], pos=(0, 0))

        self.button = wx.Button(self.p, label=u'删除', pos=(x + 330, y + 110), size=(60, -1))
        self.Bind(wx.EVT_BUTTON, self.on_click, self.button)

        self.input_text.SetFocus()

    def on_click(self, event):
        self.dicts.remove(self.dic)
        save_new_data(self.dic, self.dicts, db_file=self.db_file)
        if os.path.exists(self.path):
            os.remove(self.path)
        self.start()
        print('-deleted: ', self.dic['file_name'])

    def on_txt(self, event):
        data = self.input_text.GetValue()
        if '\n' in data:
            temp = data.replace('\n', '')
            self.dic['file_txt'] = temp if temp else self.dic['file_txt']
            self.dic['read_flag'] = True
            save_new_data(self.dic, self.dicts, db_file=self.db_file)
            self.start()

    def on_save_folder(self, event):
        path = self.save_folder
        dlg = wx.DirDialog(self, u"选择保存文件夹", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
        dlg.Destroy()
        print(path)
        self.save_folder = Path(path)
    
    def on_img_folder(self, event):
        path = self.img_folder
        dlg = wx.DirDialog(self, u"选择图片所在的文件夹", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
        dlg.Destroy()
        print(path)
        self.img_folder = Path(path)
    


if __name__ == '__main__':
    app = wx.App()
    frame = Myframe()
    frame.Show()
    app.MainLoop()
